package yuejian_07.yuejian_07.fragment;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Toast;

import com.jude.rollviewpager.RollPagerView;
import com.jude.rollviewpager.adapter.StaticPagerAdapter;
import com.jude.rollviewpager.hintview.ColorPointHintView;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import yuejian_07.yuejian_07.R;
import yuejian_07.yuejian_07.adapter.ClubListAdapter;
import yuejian_07.yuejian_07.javabean.Club;
import yuejian_07.yuejian_07.model.ClubModel;


public class ClubFragment extends Fragment {
    //声明变量
    private View view=null;
    //private ImageView search;//搜索
    private RollPagerView rollPV = null;
    private RecyclerView recyclerView;
    private List mDataList;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState)
    {
        view=inflater.inflate(R.layout.tab02_club, container, false);
        initViews();
        initEvents();
        initData();
        return view;
    }
    public void initViews(){
        //search=(ImageView)view.findViewById(R.id.search);
        rollPV = (RollPagerView) view.findViewById(R.id.rollPV);
        recyclerView= (RecyclerView)view.findViewById(R.id.rv_club);
        recyclerView.setLayoutManager(new LinearLayoutManager(this.getActivity()));
        recyclerView.setItemAnimator(new DefaultItemAnimator());
    }
    private void initData()
    {

        ClubModel clubModel = new ClubModel();
        Call call = clubModel.clublist();
        call.enqueue(new Callback<List<Club>>() {
            @Override
            public void onResponse(Call<List<Club>> call, Response<List<Club>> response) {
                //mDataList= (List) response.body();
                List<Club> data=response.body();

                System.out.print("data");
                recyclerView.setAdapter(new ClubListAdapter(getActivity(),data));
            }

            @Override
            public void onFailure(Call<List<Club>> call, Throwable t) {
                Toast.makeText(getActivity(), "fail", Toast.LENGTH_SHORT).show();
            }
        });

    }
    public void initEvents(){

        //跳转到搜索页
        /*search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), HomeSearchActivity.class);
                startActivityForResult(intent, 1);
            }
        });*/


        /****************轮播图*****************/
        rollPV.setPlayDelay(3000);
        //设置图片切换动画时间
        rollPV.setAnimationDurtion(500);
        //设置指示器:
        //rollPV.setHintView(new IconHintView());
        //rollPV.setHintView(new IconHintView(this,R.mipmap.ic_launcher,R.mipmap.ic_launcher));
        rollPV.setHintView(new ColorPointHintView(getActivity(), getResources().getColor(R.color.colorPrimary),
                Color.WHITE));
        //设置适配器
        rollPV.setAdapter(new RollPagerAdapter());

        //设置每一个图片的点击事件
        /*rollPV.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                Toast.makeText(getActivity(), "点击了图片"+position, Toast.LENGTH_SHORT).show();

            }
        });*/


    }
    private int images[] = {
            R.drawable.lunbo1,
            R.drawable.lunbo2,
            R.drawable.lunbo3,
    };

    private class RollPagerAdapter extends StaticPagerAdapter {

        @Override
        public View getView(ViewGroup container, int position) {

            ImageView view=new ImageView(container.getContext());
            //设置图片资源
            view.setImageResource(images[position]);
            //设置高度和宽度
            view.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
            //设置拉伸方式
            view.setScaleType(ImageView.ScaleType.CENTER_CROP);

            return view;
        }

        @Override
        public int getCount() {
            return images.length;
        }
    }







}